using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Matrix.Configuration.Constants;

namespace Matrix.Configuration.Configurations
{
    /// <summary>
    /// ConfigurationCacheItem实体配置
    /// </summary>
    public class ConfigurationCacheItemConfiguration : IEntityTypeConfiguration<ConfigurationCacheItem>
    {
        public void Configure(EntityTypeBuilder<ConfigurationCacheItem> builder)
        {
            builder.ToTable("ConfigurationCacheItems");

            // 配置主键
            builder.HasKey(x => x.Id);

            // 配置索引
            builder.HasIndex(x => x.CacheKey)
                .IsUnique()
                .HasDatabaseName("IX_ConfigurationCacheItems_CacheKey");

            builder.HasIndex(x => x.ExpiryTime)
                .HasDatabaseName("IX_ConfigurationCacheItems_ExpiryTime");

            // 配置属性
            builder.Property(x => x.CacheKey)
                .IsRequired()
                .HasMaxLength(ConfigurationCacheItemConsts.MaxCacheKeyLength);

            builder.Property(x => x.ValueType)
                .IsRequired()
                .HasMaxLength(ConfigurationCacheItemConsts.MaxValueTypeLength);

            builder.Property(x => x.ExpiryTime)
                .IsRequired()
                .HasDefaultValueSql("NOW() + INTERVAL '1 hour'");

            // 配置租户过滤
            builder.ConfigureByConvention();
        }
    }
}